package com.example.casper.Experiment2024.gone;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.items.Location;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class goneLocationDetailsActivity extends AppCompatActivity {

    private ImageView imageViewPic;
    private TextView textViewLocationName, textViewLatitude, textViewLongitude, textViewFeeling;
    private Button PrtScButton;
    private Button buttonShare;
    public Location location;
    private SharedViewModel sharedViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gone_location_details);

        imageViewPic = findViewById(R.id.image_view_pic);
        textViewLocationName = findViewById(R.id.text_view_location_name);
        textViewLatitude = findViewById(R.id.text_view_latitude);
        textViewLongitude = findViewById(R.id.text_view_longitude);
        textViewFeeling = findViewById(R.id.text_view_feeling);

        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        Log.d("ViewModel", "Activity ViewModel: " + sharedViewModel.toString());
        // 从 Intent 获取 Location 对象
        Intent intent = getIntent();
        location = (Location) intent.getSerializableExtra("location");
        int position = intent.getIntExtra("position", -1); // 获取位置索引
        if (location != null) {
            textViewLocationName.setText(location.getLocationName());
            textViewLatitude.setText(String.valueOf(location.getLatitude()));
            textViewLongitude.setText(String.valueOf(location.getLongitude()));
            textViewFeeling.setText(location.getFeeling());
            if (location.getPicPath() != null) {
                Glide.with(this).load(location.getPicPath()).into(imageViewPic);
            } else {
                imageViewPic.setImageResource(R.drawable.ic_launcher_background); // 设置默认图片
            }
        }



        // 设置回退按钮
        Button backButton = findViewById(R.id.button_back_to_location);
        backButton.setOnClickListener(v -> finish()); // 关闭当前 Activity

        // 截屏按钮
        PrtScButton = findViewById(R.id.button_PrtSc);
        PrtScButton.setOnClickListener(v -> captureScreen());

        // 分享按钮
        buttonShare = findViewById(R.id.button_share);
        buttonShare.setOnClickListener(v -> {
            location = (Location) getIntent().getSerializableExtra("location");
            if (location != null) {
                // 弹出对话框询问用户是否要分享
                new AlertDialog.Builder(goneLocationDetailsActivity.this)
                        .setTitle("分享旅游足迹")
                        .setMessage("您是否要分享这份旅游足迹？")
                        .setPositiveButton("是", (dialog, which) -> {
                            // 用户选择“是”，添加到分享列表并通知 ShareFragment

                            // 通知 ShareFragment 更新数据
                            sharedViewModel.setLocationUpdated(location);


                            Toast.makeText(goneLocationDetailsActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                        })
                        .setNegativeButton("否", (dialog, which) -> {
                            // 用户选择“否”，显示 Toast 消息
                            Toast.makeText(goneLocationDetailsActivity.this, "取消分享", Toast.LENGTH_SHORT).show();
                        })
                        .show();
            }
        });
    }

    // 截屏功能
    private void captureScreen() {
        View rootView = findViewById(android.R.id.content); // 获取 Activity 的根视图
        if (rootView != null) {
            Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            rootView.draw(canvas);
            saveBitmapToFile(bitmap);
        } else {
            Toast.makeText(this, "Unable to capture screen. View is not ready.", Toast.LENGTH_SHORT).show();
        }
    }

    // 保存截图到文件
    public void saveBitmapToFile(Bitmap bitmap) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        File file = new File(getExternalFilesDir(null), "screenshot_" + timeStamp + ".png");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            Toast.makeText(this, "Screenshot saved to: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Failed to save screenshot", Toast.LENGTH_SHORT).show();
        }
    }
}
